Ext.onReady(function(){ 

Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = 'under';

var sm = new Ext.grid.CheckboxSelectionModel();

var cm1=new Ext.grid.ColumnModel([ 
sm,
new Ext.grid.RowNumberer({header: "编号",width:40}),
{header:'客户编号',dataIndex:'custId', hidden:true},
{header:'客户号',dataIndex:'custNo'},
{header:'客户名称',dataIndex:'custName',width:120,resizable:false},
{header:'客户类型',dataIndex:'custType',width:60},
{header:'经办人',dataIndex:'inputUserName',width:120},
{header:'所属机构',dataIndex:'inputOrgName',width:120},
{header:'财报',  
xtype: 'actioncolumn',
width: 50,resizable:false,
  items: [{
                    icon   : 'images/arrow.gif',  
                    tooltip: 'Check report detail',
                    handler: function(grid, rowIndex, colIndex) {
                    Ext.getCmp("win").show(); 
                    var cust = ds.getAt(rowIndex);
                    var custId = cust.get('custId');
                   	ds2.proxy =new Ext.data.HttpProxy({
						url:'finance/listReport.do?custId='+custId,
						method:'POST'
					});
					ds2.load();
                    }
                }]              
}    
]); 
var ds=new Ext.data.Store({ 
				reader:new Ext.data.JsonReader({ },[
				{name:'custId'},
				{name:'custNo'},
				{name:'custName'}, 
				{name:'custType'},
				{name:'inputUserName'},
				{name:'inputOrgName'},
				]) 
});

var custTypeStore = new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showType.do?type=QueryCustType',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
]) 
});

var grid=new Ext.grid.GridPanel({ 
bodyStyle: 'padding:5px',
id:'myCustomers',
renderTo:'result_panel',
stripeRows:true,//斑马线效果
title:"查询结果列表", 
height:500, 
cm:cm1,
store: ds, 
loadMask: { msg: "加载中..." }, 
viewConfig: {   
                            forceFit:true   
            },   
            bbar: new Ext.PagingToolbar({   
                pageSize: 20,   
                store: ds,   
                displayInfo: true,   
                displayMsg: '第{0} 到 {1} 条数据 共{2}条',   
                emptyMsg: "没有数据"   
            })
});




//查找客户表单
var form = new Ext.form.FormPanel({ 
renderTo:'search_panel',     
height : 150,
width:800,
id: 'findForm', 
title: '查询客户',
anchor:'80%', 
bodyStyle: 'margin:3px;text-align:left;', 
frame: true, 
labelWidth: 100, 
items: 
[ {
	layout:"column",
items:[{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
xtype: 'combo', 
 id : "custType",
 store :custTypeStore,
fieldLabel: '客户类型',
valueField : 'code',
displayField : 'name',
emptyText : '请选择客户类型...',
allowBlank : true,
typeAhead : true,
width: 200,
mode:'remote',  
hiddenName:'custType',
editable: false,
triggerAction : 'all',
selectOnFocus : true   
}]},{ columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
xtype: 'textfield', 
id:'custName',
name: 'custName', 
fieldLabel: '客户名称',  
allowBlank: true, 
width:200
         
}]} 
]},{
	layout:"column",
items:[{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
xtype: 'textfield',
id: 'custNo', 
name: 'custNo', 
fieldLabel: '客户号', 
allowBlank: true,  
width:200   
}]},{ columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
        	   xtype: 'combo',
				id: 'orgCode',
				//name: 'exposureType', 
				fieldLabel: '所属机构', 
				store:new Ext.data.SimpleStore({fields:['id', 'text'],data:[[]]}), 
				displayField: 'text',
				valueField: 'id',
				hiddenName: 'orgCode',
	            editable:false, 
	            mode: 'local', 
	            triggerAction:'all', 
	            frame:true,
	            border:true,
	            width:200,
	            tpl: '<div id="treePanel" style="height:180px;width:240px;"></div>', //html代码      
	            emptyText:'请选择客户所属机构...',
	            resizable:true
}]} 
]},
],

buttonAlign:'right',
buttons:[{text:"查询", handler: findCustomer},
{text:"重置", handler: function(){ Ext.getCmp('findForm').form.reset(); orgCode = "";}}]
});


var cm2=new Ext.grid.ColumnModel([ 
sm,
new Ext.grid.RowNumberer({header: "编号",width:40}),
{header:'会计月份',dataIndex:'accountMonth',width:100},
{header:'报表口径',dataIndex:'reportScope',width:100},
{header:'报表类型',dataIndex:'reportKind',width:100},
{header:'财报币种',dataIndex:'reportCurrency',width:100},
{header:'审计状态',dataIndex:'auditStatus',width:100},
{header:'查看当前财务分析情况',  
xtype: 'actioncolumn',
items: [{icon   : 'images/arrow.gif',  
         tooltip: '查看报表详细信息',
         handler: function(grid2, rowIndex, colIndex) {
                        var report = ds2.getAt(rowIndex);
                        var accountMonth = report.get('accountMonth');
                        var reportScope = report.get('reportScope');
                        Ext.Ajax.request({
							  url:'checkRequest.do',
							  method:'GET',
							  success:function(){
							  window.location.href="finance/checkRecentGeneralReport.do?accountMonth="+accountMonth+"&reportScope="+reportScope;
							  },failure:function(){
							    Ext.MessageBox.alert("请求异常！");
							  }}); 
                  }
       }]              
}    
]); 

var ds2=new Ext.data.Store({  
reader:new Ext.data.JsonReader({ },[
{name:'accountMonth'}, 
{name:'reportScope'},  
{name:'reportKind'},
{name:'reportCurrency'},
{name:'auditStatus'}
]) 
});

var grid2=new Ext.grid.GridPanel({ 
id:'reportList', 
bodyStyle:'padding: 2px',
stripeRows:true,//斑马线效果
height:460, 
cm:cm2,
store: ds2,
loadMask: { msg: "加载中..." }, 
viewConfig: {   
forceFit:true   
            },   
            bbar: new Ext.PagingToolbar({   
                pageSize: 10,   
                store: ds2, 
                displayInfo: true,   
                displayMsg: '第{0} 到 {1} 条数据 共{2}条',   
                emptyMsg: "没有数据"   
            })            
});

//客户财报列表窗口 
var win = new Ext.Window({ 
id: 'win', 
title:'<div align="center">当前客户-财务报表列表</div>',
width: 880, 
height: 500, 
//背景遮罩 
modal: true, 
//重置大小 
resizable: false, 
//当关闭按钮被点击时执行的动作 
closeAction: 'hide', 
plain: true, 
items:[grid2]
});
var tree = new Ext.tree.TreePanel
({ 
    animate:true,
    titleCollapse:true,
    root:root,
    singleExpand:false,
    iconCls:"icon-tree",
    autoLoad:false,
    allowDomMove:true,
    frame:true,
    collapsible:false,
    collapsed:false,
    collapseFirst:true,
    border:false,
    listeners:{ 
    	render:function(tree){ 
    		tree.el.on('click',function(e){ 
			e.stopPropagation(); }) ;
		} 
	} 
});

var loader=new Ext.tree.TreeLoader({dataUrl:'system/showOrgTree.do'});

var root=new Ext.tree.AsyncTreeNode
({
 id:"0", 
 leaf:false,
 loader: loader,
 text:"清远农商行总行",
 expandable:true,
 expanded:true 
});         

tree.setRootNode(root);

var comboTree =  Ext.getCmp('orgCode');      
var orgCode = "";
comboTree.on('expand',function(){tree.render('treePanel');});

var tree_click = function(node,e)
{
	 e.stopEvent();   //非叶子节点则不触发
     comboTree.setValue(node.id);  //设置option值
     comboTree.setRawValue(node.text);
     //comboTree.hiddenField.value = node.id;  
 	comboTree.collapse();   //隐藏option列表
 	orgCode = comboTree.value;
};
tree.on("click",tree_click);  
//表单验证-都为空
Ext.apply(Ext.form.VTypes,   
{
 allNull: function(val, field){
 if(field.allNull){
 	var custTypeId = field.allNull.custType;
 	this.custTypeField = Ext.getCmp(custTypeId);
 	var custNameId = field.allNull.custName;
 	this.custNameField = Ext.getCmp(custNameId);
 	var orgId = field.allNull.organizationID;
 	this.orgIdField = Ext.getCmp(orgId);
 	var blId = field.allNull.busiLicenseID;
 	this.blIdField = Ext.getCmp(blId);
 	
 	var custTypeValue = this.custTypeField.getValue();
 	var custNameValue = this.custNameField.getValue();
 	var orgIdValue = this.orgIdField.getValue();
 	var blIdValue = this.blIdField.getValue();
 } 
 if(custTypeValue==null&&custNameValue==null&&orgIdValue==null&&blIdValue==null){
 	Ext.MessageBox.show({
                     title: '提示',
                     msg: '请输入至少一项查询条件！',
                     buttons: Ext.Msg.OK
                });
 	return false;
 }
 else{
 	return true;
 }
 }
});
//查询客户 
function findCustomer() { 
	var findForm = Ext.getCmp("findForm");
	
	if(findForm.form.isValid()){
	 findForm.form.doAction('submit', { 
                    url : 'finance/findCustomer.do?inputOrgId='+orgCode,
                    method : 'post', 
                    success : function(form,action) { 
                    var resultCust = Ext.util.JSON.decode(action.response.responseText).result;
                    
                    if(JSON.stringify(resultCust)!='[]'){
                    ds.proxy = new Ext.data.MemoryProxy(resultCust);
					ds.load({params:{start:0,limit:20}});
					}
					else{
					ds.proxy = new Ext.data.MemoryProxy(resultCust);
					ds.load();
					Ext.Msg.alert('提示', '当前客户不存在！');}
                    }, 
                        failure : function() { 
						Ext.Msg.alert('提示', '请求异常，请重新查询！'); 
					} 
                    }); 
}
} 

});